<?php
require_once 'widgets/TSkinnableWidget.inc';

class TBannerWidget extends TSkinnableWidget {
	protected $_conf_banners_mask_;
	protected $_conf_links_file_;
	protected $_int_banners_limit_ = 5;
	protected $_bool_rotate_ = false;
	protected $_int_rotation_interval_ = 5;
	
	private $_imgs_ = null;
	private $_links_ = null;
	
	protected function checkSource($src){
		return false;
	}	
	
	public function __set($nm,$value){
		switch ($nm){
			case 'Links':$this->links = TConvertions::ConvertToArray($value,array(';','=',',',':'),array(false,true,false,true));break;
			default:parent::__set($nm,$value);break;
		}
	}
	
	private function _get_list(){
		if (is_null($this->_imgs_)){
			$this->_imgs_ = array();
			for ($i = 1; $i <= $this->BannersLimit; $i++){
				$f = str_replace('#', $i, $this->BannersMask);
				if (file_exists($this->response->ThemeEngine->ThemeDir().'/'.$f))
					$this->_imgs_[] = $this->response->ThemeEngine->ThemeUrl().'/'.$f.'?t='.filemtime($this->response->ThemeEngine->ThemeDir().'/'.$f);
			}
			
			$this->_links_ = array();
			if (file_exists($this->response->ThemeEngine->ThemeDir().'/'.$this->LinksFile)){
				$text = file_get_contents($this->response->ThemeEngine->ThemeDir().'/'.$this->LinksFile);
				$lnk = TConvertions::ConvertToArray($text,array("\n",':'),array(false,true));
				foreach ($lnk as $slug=>$page)
					$this->_links_[] = $this->response->Service()->Url($page,null,null,array('id'=>$slug));
			}
				
		}
	}
	
	public function __get($nm){
		switch ($nm){
			case 'Images':{$this->_get_list();return $this->_imgs_;}break;
			case 'BannerLinks':{$this->_get_list();return $this->_links_;}break;
			default:return parent::__get($nm);break;
		}
	}
	
	public function defaultOutput(IThemeEngine $engine){
		
		if ($this->Rotate){
			?>
			<div id="<?php echo $this->Name(); ?>" class="banner">
				<?php 
				$n = count($this->Images);
				for ($i = 0; $i < $n; $i++){
				?>
				<a href="<?php echo isset($this->BannerLinks[$i])?$this->BannerLinks[$i]:""; ?>"><img src="<?php echo $this->Images[$i]; ?>" /></a>
				<?php 
				} 
				?>
			</div>
			<script type="text/javascript">
				$(document).ready(function(){
					var w = $("#<?php echo $this->Name(); ?>");
					w.css("overflow","hidden").css("white-space","nowrap");
					a = $("a",w);
					a.css("display","inline-block").css("position","relative").hide();
					curind = Math.floor(a.length*Math.random());
					$(a[curind]).show();
					window.setInterval(function(){
						$(a[curind]).fadeOut("slow",function(){
							curind++;
							if (curind  >= a.length) curind = 0;
							$(a[curind]).fadeIn("slow");
						});
					},<?php echo $this->RotationInterval*1000; ?>);
				});
			</script>
			<?php	
		} else {
			$imind = 0;
			$n = count($this->Images);
			if ($n > 1) $imind = rand(0, $n - 1);
		
			if (empty($lnk))
				$this->BannerLinks[] = $this->response->Service()->Url();
			$linkind = ($imind < count($this->BannerLinks))?$imind:0;
			?>
			<div class="banner" style="background-image:url(<?php echo $this->Images[$imind]; ?>);">
				<a href="<?php echo $this->BannerLinks[$linkind]; ?>">&nbsp;</a>
			</div>
			<?php
		}
	}
}